package com.example.demo.ui;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.example.demo.Account;
import com.example.demo.MainActivity;
import com.example.demo.R;
import com.example.demo.SettingActivity2;
import com.google.android.material.snackbar.Snackbar;

public class LoginActivity extends AppCompatActivity {
    LinearLayout layout;

    private Button btn_login;
    private Snackbar snackbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);


        //找到控件
       Button btn_login = findViewById(R.id.login_button);
       EditText et_username = findViewById(R.id.phone);
        EditText et_password = findViewById(R.id.et_password);
         layout = (LinearLayout)findViewById(R.id.layout_s);
       //set button onclick 并监听事件
       btn_login.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               //the page transform
               //1.指定要跳转的页面
               //第一个参数：context 上下文，运行的环境
               //第二个参数：TargetActivity 要跳转的Activity
               Context context = LoginActivity.this;
               //给intent   跳转环境、跳转的页面
               Intent intent = new Intent(context, SettingActivity2.class);

               //获取activity中的电话号码
               String username = et_username.getText().toString();
               String userPassword = et_password.getText().toString();

               snackbar = Snackbar.make(btn_login,"确定",Snackbar.LENGTH_SHORT);
               snackbar.show();
               //snakbar();
               //popupw();
               //showAlterDialog();
               if(TextUtils.isEmpty(username) || TextUtils.isEmpty(userPassword))
               {
                   Toast.makeText(LoginActivity.this,
                           "用户名或者密码不能为空！", Toast.LENGTH_SHORT).show();
               }else {


                   Account account = new Account();
                   account.setPhone(username);
                   account.setPwd(userPassword);
                   intent.putExtra("account", account);
                   startActivity(intent);

               }

               //--------------------------------------------------


           }
       });



}

    /**
     * 普通dialog
     */
    private void showAlterDialog(){
        final AlertDialog.Builder alterDiaglog = new AlertDialog.Builder(LoginActivity.this);
        alterDiaglog.setIcon(R.drawable.touxiang);//图标
        alterDiaglog.setTitle("简单的dialog");//文字
        alterDiaglog.setMessage("生存还是死亡");//提示消息
        //积极的选择
        alterDiaglog.setPositiveButton("生存", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(LoginActivity.this,
                        "点击了生存",
                        Toast.LENGTH_SHORT).show();
            }
        });
        //消极的选择
        alterDiaglog.setNegativeButton("死亡", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(LoginActivity.this,"点击了死亡",Toast.LENGTH_SHORT).show();
            }
        });
        //中立的选择
        alterDiaglog.setNeutralButton("不生不死", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(LoginActivity.this,"点击了不生不死",Toast.LENGTH_SHORT).show();
            }
        });

        //显示
        alterDiaglog.show();
    }


    public void popupw(){

        TextView tv = new TextView(this);
        tv.setTextSize(20);
        tv.setText("哥是弹出窗体");
        tv.setTextColor(Color.RED);
        //PopupWindow popupWindow = new PopupWindow(tv, 100, 100);
        PopupWindow popupWindow = new PopupWindow(tv, 100, 100, true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        popupWindow.showAtLocation(layout, Gravity.LEFT | Gravity.TOP, 50,100);






    }

    public void snakbar(){
        //"aaaa"即为Snackbar左侧显示内容，setAction可以设计点击事件
        snackbar = Snackbar.make(btn_login,"确定",Snackbar.LENGTH_SHORT);
        snackbar.show();

    }





}
